package com.iwhalecloud.bss.kite.cucc.service.spec.query;

import com.iwhalecloud.bss.kite.client.api.spec.query.IQueryProdSpecService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.spec.AttrDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.IntegralProductDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.MktResProductDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.ProdCatalogDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.ProdRelDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.ProductDTO;
import com.iwhalecloud.bss.kite.common.cache.AbstractCache;
import com.iwhalecloud.bss.kite.common.callclient.CallClient;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.LabelConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotNull;
import com.iwhalecloud.bss.kite.dataservice.api.IQueryProductService;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.MktResProdInfo;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.ProdAttr;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.ProdCatalog;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.ProdRelInfo;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.MktResourceMapper;
import com.iwhalecloud.bss.kite.service.order.DepService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.I18nUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.common.util.StringUtil;
import com.ztesoft.bss.svcability.aggobj.SvcCont;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class QueryProdSpecService implements IQueryProdSpecService {

    @Autowired
    private IQueryProductService queryProductService;

    /*
     * @Resource private ProdAttrMapper prodAttrMapper;
     * @Resource private ProdRelMapper prodRelMapper;
     * @Resource private ProdAttrExtMapper prodAttrExtMapper;
     */
    /***
     * 查询产品基本信息<br>
     * 产品ID可以是接入产品、功能产品、终端产品
     *
     * @author th<br>
     * @param prodId 产品ID 不能为null
     * @return <br>
     */
    @Override
    public DataResult<ProductDTO> getProductInfo(String prodId) {
        Product product = queryProductService.getProduct(prodId);
        if (product == null) {
            ErrorConsts.PRODUCT_NOT_EXISTS.throwOut(prodId);
        }

        ProductDTO productDTO = new ProductDTO();
        KiteBeanUtils.copyProperties(product, productDTO);
        return ErrorConsts.SUCCESS.getResult(productDTO);
    }

    /**
     * 根据产品ID查询产品属性集合 <br>
     * 产品ID可以是接入产品、功能产品、终端产品
     *
     * @author th<br>
     * @param prodId
     * @return <br>
     */
    @Override
    public DataResult<List<AttrDTO>> listProdAttrs(String prodId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        // 从聚合对象去取该产品的拓展属性对象
        Product prod = ProductCache.get(prodId);
        List<ProdAttr> attrList = prod.listProdAttrExts();
        // List<ProdAttrExt> attrList = prodAttrExtMapper.selectAllByProdId(prodId);
        /*
         * List<AttrDTO> attrList = prodAttrMapper.selectAllByProdId(prodId); for (AttrDTO attr : attrList) {
         * attr.setMustShow(attr.getIsMake()); }
         */
        List<AttrDTO> result = KiteBeanUtils.copyPropsForList(attrList, AttrDTO.class);
        for (AttrDTO attr : result) {
            attr.setMustShow(attr.getIsMake());
        }
        return ErrorConsts.SUCCESS.getResult(result);
    }

    /**
     * 根据产品ID获取一个完整产品聚合对象信息
     *
     * @author th<br>
     * @param prodId
     * @return <br>
     */
    @Override
    public DataResult<IntegralProductDTO> getIntegralProduct(String prodId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");

        // 拿到基本产品信息
        Product prod = ProductCache.get(prodId);
        ProductDTO product = KiteBeanUtils.transform(prod, ProductDTO.class);
        // 拿到产品属性集合
        List<ProdAttr> attrList = prod.listProdAttrExts();
        // List<ProdAttrExt> attrList = prodAttrExtMapper.selectAllByProdId(prodId);
        List<AttrDTO> attrExtList = KiteBeanUtils.copyPropsForList(attrList, AttrDTO.class);
        for (AttrDTO attr : attrExtList) {
            attr.setMustShow(attr.getIsMake());
        }

        // 拿到作为A端时的Z端产品信息集合
        List<Product> zProdRelList = prod.listProdRelsByA(null);
        Map<String, List<ProductDTO>> aProdRelMap = new HashMap<String, List<ProductDTO>>();
        aProdRelMap.put("zProdRel", KiteBeanUtils.copyPropsForList(zProdRelList, ProductDTO.class));

        // 拿到作为Z端时的A端产品信息集合
        List<Product> aProdRelList = prod.listProdRelsByZ(null);
        Map<String, List<ProductDTO>> zProdRelMap = new HashMap<String, List<ProductDTO>>();
        zProdRelMap.put("aProdRel", KiteBeanUtils.copyPropsForList(aProdRelList, ProductDTO.class));

        IntegralProductDTO result = new IntegralProductDTO();
        result.setProductInfo(product);
        result.setProductAttrs(attrExtList);
        result.setProdRelAProds(aProdRelMap);
        result.setProdRelZProds(zProdRelMap);
        return ErrorConsts.SUCCESS.getResult(result);
    }

    /**
     * Description: <br>
     * 根据产品ID作为A端、Z端查询与之相关的产品关系集合
     *
     * @author th<br>
     * @param prodId 产品ID不能为空
     * @return <br>
     */
    @Override
    public DataResult<List<ProdRelDTO>> listProdRels(String prodId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        // 从聚合对象去取该产品的拓展属性对象
        Product prod = ProductCache.get(prodId);
        List<ProdRelInfo> prodRelList = prod.listProdRels();
        List<ProdRelDTO> result = KiteBeanUtils.copyPropsForList(prodRelList, ProdRelDTO.class);
        // List<ProdRelDTO> result = prodRelMapper.selectProdRelByProdIdAndRelType(prodId, null);
        for (ProdRelDTO prodRel : result) {
            prodRel.setRelTypeName(getRelTypeName(prodRel.getRelType()));
        }
        return ErrorConsts.SUCCESS.getResult(result);
    }

    /**
     * 根据产品关系类型算出产品关系类型的名称
     *
     * @author th<br>
     * @param relType
     * @return relTypeName<br>
     */
    public String getRelTypeName(String relType) {
        String relTypeName = AttrValueCache.getAttrValueNameByAttrNbrAndValue(KeyConsts.PRD_0007, relType);
        if (KiteStringUtils.isBlank(relTypeName)) {
            relTypeName = I18nUtil.getMessage("BSS_KITE_INST_90012");
        }
        return relTypeName;

    }

    /**
     * 根据产品ID、产品关系类型查询产品关系集合
     *
     * @author th<br>
     * @param prodId
     * @param relType
     * @return <br>
     */
    @Override
    public DataResult<List<ProdRelDTO>> listProdRelsByRelType(String prodId, String relType) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        KiteObjectUtils.checkParamIsEmpty(relType, "relType");
        // 获得当前产品的实例
        Product prod = ProductCache.get(prodId);
        // 获得当前产品的关系集合通过relType筛选
        List<ProdRelInfo> prodRelList = prod.listProdRels(relType);
        List<ProdRelDTO> result = KiteBeanUtils.copyPropsForList(prodRelList, ProdRelDTO.class);
        // 填充DTO中DO不存在的数据
        for (ProdRelDTO prodRel : result) {
            prodRel.setRelTypeName(getRelTypeName(prodRel.getRelType()));
        }
        return ErrorConsts.SUCCESS.getResult(result);
    }

    /**
     * 根据A端产品ID、产品关系查询产品关系Z端产品信息集合
     *
     * @author th<br>
     * @param aProdId A端产品ID (不能为空)
     * @param relType 产品关系 (不能为空)
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listProdRelsByAProd(String aProdId, String relType) {
        KiteObjectUtils.checkParamIsEmpty(aProdId, "aProdId");
        KiteObjectUtils.checkParamIsEmpty(relType, "relType");
        // 获得当前产品的实例
        Product prod = ProductCache.get(aProdId);
        // 获得当前产品为A端的关系集合
        List<Product> prodRelList = prod.listProdRelsByA(relType);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(prodRelList, ProductDTO.class));
    }

    /**
     * 根据Z端产品ID、产品关系查询产品关系A端产品信息集合
     *
     * @author th<br>
     * @param zProdId Z端产品ID (不能为空)
     * @param relType 产品关系 (不能为空)
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listProdRelsByZProd(String zProdId, String relType) {
        KiteObjectUtils.checkParamIsEmpty(zProdId, "zProdId");
        KiteObjectUtils.checkParamIsEmpty(relType, "relType");
        // 获得当前产品的实例
        Product prod = ProductCache.get(zProdId);
        List<Product> prodRelList = prod.listProdRelsByZ(relType);

        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(prodRelList, ProductDTO.class));
    }

    /**
     * 通过产品的Z端关系集合查询A端产品集合
     *
     * @param prodId
     * @param prodIds
     * @param relTypes
     * @return
     */
    @Validated
    public DataResult<List<ProductDTO>> queryProdRelsZByRelTypeAndProdId(@KiteNotBlank String prodId,
        @KiteNotNull List<String> prodIds, @KiteNotNull List<String> relTypes) {
        // 获得当前产品的实例
        Product prod = ProductCache.get(prodId);
        List<ProdRelInfo> prodRelInfoList = prod.listProdRelsZByRelTypeAndProdId(prodIds, relTypes);
        List<ProductDTO> productDTOS = new ArrayList<>();
        for (ProdRelInfo prodRelInfo : prodRelInfoList) {
            Product product = ProductCache.get(prodRelInfo.getaProdId());
            if (!KiteObjectUtils.isEmpty(product)) {
                ProductDTO dto = KiteBeanUtils.transform(product, ProductDTO.class);
                dto.setApplyRegionId(prodRelInfo.getApplyRegionId());
                productDTOS.add(dto);
            }
        }
        return ErrorConsts.SUCCESS.getResult(productDTOS);
    }

    /************************************** 功能产品 begin ************************************/

    /**
     * 根据销售品ID和销售品明细ID规格获取对应缺省功能产品集合
     *
     * @author th<br>
     * @param offerId 销售品ID（不能为null）
     * @param offerDetailId 明细ID（不能为null）
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listDefaultFuncProds(String offerId, String offerDetailId) {
        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(offerDetailId, "offerDetailId");
        Offer offer = OfferCache.get(offerId);
        List<Product> defaultFuncProds = offer.listDefaultFuncProds(offerDetailId);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(defaultFuncProds, ProductDTO.class));
    }

    @Override
    public DataResult<List<ProductDTO>> listDefaultFuncProds(String offerId, String roleId, String prodId) {
        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(roleId, "roleId");
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        Offer offer = OfferCache.get(offerId);
        List<Product> defaultFuncProds = offer.listDefaultFuncProds(roleId, prodId);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(defaultFuncProds, ProductDTO.class));
    }

    /**
     * 根据销售品ID和销售品明细ID规格获取对应必选功能产品集合
     *
     * @author th<br>
     * @param prodId 产品ID（不能为null)
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listMustFuncProds(String offerId, String roleId, String prodId) {
        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(roleId, "roleId");
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        Offer offer = OfferCache.get(offerId);
        List<Product> mustFuncProds = offer.listMustFuncProds(roleId, prodId);

        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(mustFuncProds, ProductDTO.class));
    }

    @Override
    public DataResult<List<ProductDTO>> listMustFuncProds(String offerId, String offerDetailId) {
        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(offerDetailId, "offerDetailId");
        Offer offer = OfferCache.get(offerId);
        List<Product> mustFuncProds = offer.listMustFuncProds(offerDetailId);

        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(mustFuncProds, ProductDTO.class));
    }

    /**
     * 获取主产品可选的功能产品集合 <br>
     *
     * @author th<br>
     * @param prodId 主产品ID（不能为空）
     * @param lanId 本地网ID
     * @param provId 全省区域编号
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listFuncProds(String prodId, String lanId, String provId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        KiteObjectUtils.checkParamIsEmpty(lanId, "lanId");
        KiteObjectUtils.checkParamIsEmpty(provId, "provId");
        // 获得当前产品的实例
        Product prod = ProductCache.get(prodId);
        List<Product> productInfos = prod.listFuncProds(lanId, provId);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class));
    }

    /**
     * 获取主产品的功能产品目录集合
     *
     * @author th<br>
     * @param prodId 主产品ID （不能为空）
     * @return <br>
     */
    @Override
    public DataResult<List<ProdCatalogDTO>> listFuncProdCatalogs(String prodId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");

        // 获得当前产品的实例
        Product prod = ProductCache.get(prodId);
        List<ProdCatalog> prodCatalogs = prod.listProdCatalogs();
        List<ProdCatalogDTO> result = KiteBeanUtils.copyPropsForList(prodCatalogs, ProdCatalogDTO.class);
        return ErrorConsts.SUCCESS.getResult(result);
    }

    /**
     * 根据主产品ID、功能产品名称查询主产品可选功能产品集合
     *
     * @author th<br>
     * @param prodId 主产品ID 不能为空
     * @param prodName 功能产品名称
     * @param lanId 本地网ID
     * @param provId 全省区域编号
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listFuncProdsByProdName(String prodId, String prodName, String lanId, String provId) {
        return listFuncProdsByProdName(prodId, prodName, lanId, provId, null, null);
    }

    @Override
    public DataResult<List<ProductDTO>> listFuncProdsByProdName(String prodId, String prodName, String lanId, String provId, String regionId, String staffId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        Product prod = ProductCache.get(prodId);
        if (Objects.isNull(prod)) {
            return ErrorConsts.SUCCESS.getResult(Collections.emptyList());
        }
        List<Product> productInfos = prod.listFuncProdsByProdName(prodName, lanId, provId, regionId, staffId);
        filterFuncProductByLabel(productInfos);

        if (StringUtil.isEmpty(prodName)) {
            // 如果不传搜索词则返回热销产品
            AbstractCache abstractCache = SpringUtil.getBean(KiteStringUtils.uncapitalize("AttrValueCache"));
            List<AttrValue> hotProds = abstractCache.defaultGetObject("994001451");
            if (KiteListUtils.isEmpty(hotProds) || KiteListUtils.isEmpty(productInfos)) {
                return ErrorConsts.SUCCESS.getResult(Collections.emptyList());
            }
            else {
                List<Product> hotProductInfos = new ArrayList<>();
                for (Product product : productInfos) {
                    // 如果是热销产品则放入返回对象
                    if (isHotProd(hotProds, product.getProdId())) {
                        hotProductInfos.add(product);
                    }
                }
                return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(hotProductInfos, ProductDTO.class));
            }
        }

        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class));
    }

    /**
     * 隐藏特定打标的功能产品
     * @param funcProductList
     */
    private void filterFuncProductByLabel(List<Product> funcProductList) {
        Iterator<Product> iterator = funcProductList.iterator();
        while (iterator.hasNext()) {
            Product funcProdInst = iterator.next();
            if (funcProdInst.hasFeture(new ArrayList<String>(Arrays.asList(LabelConsts.PRODUCT_HIDE_LABEL_ID)))) {
                iterator.remove();
            }
        }
    }

    /**
     * 判断是否为热销产品
     *
     * @param hotProds
     * @param prodId
     * @return
     */
    private boolean isHotProd(List<AttrValue> hotProds, String prodId) {
        boolean isHotProd = false;
        for (AttrValue attrValue : hotProds) {
            if (prodId.equals(attrValue.getAttrValue())) {
                isHotProd = true;
                break;
            }
        }
        return isHotProd;
    }

    /**
     * 根据主产品ID、功能产品目录查询主产品下可选功能产品集合
     *
     * @author th<br>
     * @param prodId
     * @param prodCatType
     * @param lanId
     * @param provId
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listFuncProdsByProdCatType(String prodId, String prodCatType, String lanId,
        String provId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodName");
        KiteObjectUtils.checkParamIsEmpty(prodCatType, "prodCatType");
        // 获得当前产品的实例
        Product prod = ProductCache.get(prodId);
        List<Product> productInfos = prod.listFuncProdsByCatType(prodCatType, lanId, provId);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class));
    }

    /************************************** 功能产品 end ************************************/

    /************************************** 终端产品 begin ************************************/

    /**
     * 根据销售品ID、销售品明细ID规格获取对应缺省终端产品集合
     *
     * @author th<br>
     * @taskId <br>
     * @param detailId 销售品明细ID 不能为空
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listDefaultResProds(String offerId, String detailId) {
        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(detailId, "detailId");
        Offer offer = OfferCache.get(offerId);
        List<Product> productInfos = offer.listDefaultResProds(detailId);
        List<ProductDTO> productDTOs = KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class);
        return ErrorConsts.SUCCESS.getResult(productDTOs);
    }

    /**
     * 根据销售品明细ID规格获取对应必选终端产品集合
     *
     * @author th<br>
     * @param detailId 销售品明细ID 不能为空
     * @return <br>
     */
    @Override
    public DataResult<List<ProductDTO>> listMustResProds(String offerId, String detailId) {

        KiteObjectUtils.checkParamIsEmpty(detailId, "detailId");
        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");

        Offer offer = OfferCache.get(offerId);
        List<Product> productInfos = offer.listMustResProds(detailId);
        List<ProductDTO> productDTOs = KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class);
        return ErrorConsts.SUCCESS.getResult(productDTOs);
    }

    /**
     * 获取主产品可以选择终端产品资源集合
     *
     * @author th<br>
     * @param prodId 不能为null
     * @param lanId
     * @param provId
     * @return <br>
     */
    @Override
    public DataResult<List<MktResProductDTO>> listMktResProds(String prodId, String lanId, String provId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        Product product = ProductCache.get(prodId);
        List<MktResProdInfo> mktResProdInfos = product.getMktResProdByProdId(lanId, provId);
        // List<MktResProdInfo> mktResProdInfos = Offer.getMktResProdInfos(offerDetailId, lanId, provId);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(mktResProdInfos, MktResProductDTO.class));
    }

    /**
     * 根据终端名称查询当前主产下能购买终端资源集合
     *
     * @author th<br>
     * @param prodId
     * @param resName
     * @param lanId
     * @param provId
     * @param staffId 现无作用
     * @return <br>
     */
    @Override
    public DataResult<List<MktResProductDTO>> listMktResProdsByResName(String prodId, String resName, String lanId,
        String provId, String staffId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        KiteObjectUtils.checkParamIsEmpty(lanId, "lanId");
        KiteObjectUtils.checkParamIsEmpty(provId, "provId");
        // List<MktResProdInfo> mktResProdInfos = Offer.getMktResProdInfosByName(offerDetailId, resName, lanId, provId);
        Product product = ProductCache.get(prodId);
        List<MktResProdInfo> mktResProdInfos = product.getMktResProdByProdIdAndResName(resName, lanId, provId);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(mktResProdInfos, MktResProductDTO.class));
    }

    /**
     * 根据终端串码查询主产品能订购终端资源
     *
     * @author th<br>
     * @param prodId
     * @param mktResCode
     * @param lanId
     * @param provId
     * @param staffId
     * @return <br>
     */
    @SuppressWarnings("unchecked")
    @Override
    public DataResult<List<MktResProductDTO>> getMktResProdByResCode(String prodId, String mktResCode, String lanId,
        String provId, String staffId) {
        KiteObjectUtils.checkParamIsEmpty(prodId, "prodId");
        KiteObjectUtils.checkParamIsEmpty(mktResCode, "mktResCode");
        KiteObjectUtils.checkParamIsEmpty(lanId, "lanId");
        KiteObjectUtils.checkParamIsEmpty(provId, "provId");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("businessId", provId);
        paramMap.put("resInstNbr", mktResCode);
        // SvcCont outSvcCont = CallClient.callRemoteService(CallClientEnum.QRY_ENTITY_INFO_BY_RES_INST_NBR, paramMap);
        List<Map<String, Object>> mktResList = queryMktResProdByResCode(mktResCode);
        if (KiteListUtils.isEmpty(mktResList)) {
            return ErrorConsts.SUCCESS.getResult(null);
        }
        Product product = ProductCache.get(prodId);
        List resIds = new ArrayList<String>();
//        resIds = mktResList.stream().map(map -> map.get("salesResourceId")).collect(Collectors.toList());

        resIds.add(String.valueOf(mktResList.get(0).get("termtypeid")));
        List<MktResProdInfo> mktResProdInfos = product.getMktResProdByProdIdAndResIds(resIds, lanId, provId);
        if (KiteListUtils.isEmpty(mktResProdInfos)) {
            ErrorConsts.SUCCESS.getResult(null);
        }
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(mktResProdInfos, MktResProductDTO.class));
    }

    @SuppressWarnings("unchecked")
    private List<Map<String, Object>> queryMktResProdByResCode(String mktResCode) {

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> requetMap = new HashMap<String, Object>();
        SvcCont svcCont = null;
        String staffId = ContextUtil.getLoginStaffId().toString();
        //资源实例串号
        requetMap.put("resInstNbr", mktResCode);
        //用户标识
        requetMap.put("staffID", staffId);
        //资源类别编码
        requetMap.put("resTypeNbr", "101000000");
        //是否预判    1：是；0：否
        requetMap.put("toCheck", "0");
        // 预判类型    toCheck为1时必填
        requetMap.put("checkType", "");
        requetMap.put("AppKey", "6003020033");
        requetMap.put("DstSysID", "6003020001");
        requetMap.put("Sign", "123");
        requetMap.put("ReqTime", DepService.getReqTime());
        requetMap.put("TransactionID", DepService.genRequestTransactionId());
        try {
            svcCont = CallClient.extCall("qry.rcEntity.crm", requetMap);
            if (svcCont != null && !"0000".equals(svcCont.getResultCode()) && !SvcCont.RESULT_CODE_SUCCESS.equals(svcCont.getResultCode())) {
                return list;
            }
            if (svcCont != null && svcCont.getResultObject() != null && svcCont.getResultObject().getData() != null) {
                Map<String, Object> map = svcCont.getResultObject().getData();
                if (map.containsKey("SvcCont") && ((Map<String, Object>) map.get("SvcCont")).containsKey("resultObject")) {
                    Map<String, Object> result = (Map<String, Object>) ((Map<String, Object>) map.get("SvcCont")).get("resultObject");
                    if (result.containsKey("resInstInfo") && !KiteMapUtils.isEmpty((Map<String, Object>) result.get("resInstInfo"))) {
                        Map<String, Object> resInstInfo = (Map<String, Object>) result.get("resInstInfo");
                        list.add(resInstInfo);
                    }
                }
            }
        }
        catch (Exception e) {
            return list;
        }
        return list;

    }

    @Override
    public DataResult<List<ProductDTO>> listDefaultResProds(String offerId, String roleId, String objId) {

        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(roleId, "roleId");
        KiteObjectUtils.checkParamIsEmpty(objId, "objId");
        Offer offer = OfferCache.get(offerId);
        List<Product> productInfos = offer.listDefaultResProds(roleId, objId);
        List<ProductDTO> productDTOs = KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class);
        return ErrorConsts.SUCCESS.getResult(productDTOs);

    }

    @Override
    public DataResult<List<ProductDTO>> listMustResProds(String offerId, String roleId, String objId) {

        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(roleId, "roleId");
        KiteObjectUtils.checkParamIsEmpty(objId, "objId");
        Offer offer = OfferCache.get(offerId);
        List<Product> productInfos = offer.listMustResProds(roleId, objId);
        List<ProductDTO> productDTOs = KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class);
        return ErrorConsts.SUCCESS.getResult(productDTOs);
    }

    @Override
    public DataResult<List<ProductDTO>> listChoosableResProds(String offerId, String offerObjRelId) {

        KiteObjectUtils.checkParamIsEmpty(offerId, "offerId");
        KiteObjectUtils.checkParamIsEmpty(offerObjRelId, "offerObjRelId");
        Offer offer = OfferCache.get(offerId);
        List<Product> productInfos = offer.listChoosableResProds(offerObjRelId);
        List<ProductDTO> productDTOs = KiteBeanUtils.copyPropsForList(productInfos, ProductDTO.class);
        return ErrorConsts.SUCCESS.getResult(productDTOs);
    }

    @Override
    public DataResult<List<MktResProductDTO>> listMkteResByMktResNbr(String prodId, String mktResNbr, String lanId, String provId, String staffId) {
        List<MktResProdInfo> mktResProdInfos = SpringUtil.getBean(MktResourceMapper.class).listMkteResByMktResNbr(prodId, mktResNbr, lanId, provId, staffId);
        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(mktResProdInfos, MktResProductDTO.class));
    }

    /************************************** 终端产品 end ************************************/
}
